let MS = require("./ms.js")
const request = require("./request.js")
const Error = require('./error')
const storage = require("./storage.js")
const {
    isObject,
    isString,
    isNumber,
    isUndefined,
    isArray
} = require('./dataType')

const guilds = class guilds {
    constructor() {

    }

    query(options) {
        let params = {
            pageNum: 1,
            pageSize: 10
        }
        if(!isUndefined(options.memberId) && isString(options.memberId)) {
            params.memberId = options.memberId;
        }
        if(!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name;
        }
        if(!isUndefined(options.pageNum) && isNumber(options.pageNum)) {
            params.pageNum = options.pageNum;
        }
        if(!isUndefined(options.pageSize) && isNumber(options.pageSize)) {
            params.pageSize = options.pageSize;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.FIND_GUILDS.url,
                MS._config.parameters.GUILDS.FIND_GUILDS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    get(id) {
        let params = {
            id: id
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.GET_GUILDS.url,
                MS._config.parameters.GUILDS.GET_GUILDS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    getGuildsMember(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.guildsId) && isString(options.guildsId)) {
            params.guildsId = options.guildsId;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.GET_GUILDS_MEMBER.url,
                MS._config.parameters.GUILDS.GET_GUILDS_MEMBER.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    getGuildsByUserId(userId) {
        let params = {
            userId: userId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.GET_GUILDS_BY_USER_ID.url,
                MS._config.parameters.GUILDS.GET_GUILDS_BY_USER_ID.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    createGuilds(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.name) && isString(options.name)) {
            params.name = options.name;
        }
        if(!isUndefined(options.synopsis) && isString(options.synopsis)) {
            params.synopsis = options.synopsis;
        }
        if(!isUndefined(options.logo) && isString(options.logo)) {
            params.logo = options.logo;
        }
        if(!isUndefined(options.applyAudit) && isBoolean(options.applyAudit)) {
            params.applyAudit = options.applyAudit;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.CREATE_GUILDS.url,
                MS._config.parameters.GUILDS.CREATE_GUILDS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    joinGuilds(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.guildsId) && isString(options.guildsId)) {
            params.guildsId = options.guildsId;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.JOIN_GUILDS.url,
                MS._config.parameters.GUILDS.JOIN_GUILDS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    quitGuilds(options) {
        let params = {}
        if(!isUndefined(options.userId) && isString(options.userId)) {
            params.userId = options.userId;
        }
        if(!isUndefined(options.guildsId) && isString(options.guildsId)) {
            params.guildsId = options.guildsId;
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.QUIT_GUILDS.url,
                MS._config.parameters.GUILDS.QUIT_GUILDS.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }

    findGuildsMemberInfo(guildsId) {
        let params = {
            guildsId: guildsId
        }
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.GUILDS.FIND_USER_BY_GUILDS_ID.url,
                MS._config.parameters.GUILDS.FIND_USER_BY_GUILDS_ID.method, params).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err)
            })
        })
    }
}

module.exports = guilds